package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.annotation.Authorization;
import com.csun.cmny.provider.dto.ExamineDto;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.ExamineService;
import com.csun.cmny.provider.service.OldmanService;
import com.csun.cmny.provider.utils.MapUtils;
import com.csun.cmny.provider.utils.ValidateUtil;
import com.csun.cmny.provider.validator.ExamineValid;
import com.csun.cmny.provider.vo.ExamineManVo;
import com.csun.cmny.provider.vo.ExamineVo;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Examine")
public class ExamineController extends BaseController {

    @Resource
    private ExamineService examineService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OldmanService oldmanService;

    /**
     * 添加体检记录接口
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "/addExamineRecord", method = RequestMethod.POST)
    public Wrapper addExamineRecord(@Validated(value = {ExamineValid.class}) ExamineDto examine, BindingResult br, HttpServletRequest request) {
        /* 封装需要进行数据校验的字段 */
        List<String> params = new ArrayList<String>();
        params.add("height");
        params.add("weight");
        params.add("SBP");
        params.add("DBP");
        params.add("examinetime");
        params.add("oldmanid");
        params.add("examineresult");
        params.add("examinephoto");

        String paramValid[] = new String[params.size()];
        for (int i = 0, j = params.size(); i < j; i++) {
            paramValid[i] = params.get(i);
        }
        /* 存放校验错误结果集 */
        Map<String, String> errorMap = ValidateUtil.getErrorMap(br, paramValid);

        if (errorMap != null && errorMap.size() != 0) {
            /* 返回参数校验错误详情 */
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, MapUtils.getFirstOrNull(errorMap), null);
        }
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();

        //获取老人名字
        Oldman oldman = new Oldman();
        oldman.setOldmanId(examine.getOldmanid());
        Oldman old = oldmanService.selectByOld(oldman);

        examine.setOldManName(old.getOldName());
        examine.setGroupId(homeId);
        examine.setUserId(loginAuthDto.getUserId());
        examine.setUserName(loginAuthDto.getUserName());

        int n = examineService.insert(examine);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.add.fail", null), 0);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("comment.add.success", null), n);
    }


    /**
     * 删除体检记录接口
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "/delExamineRecord", method = RequestMethod.POST)
    public Wrapper delExamineRecord(
            @Validated(value = {ExamineValid.class}) ExamineDto examine, BindingResult br, HttpServletRequest request) {


        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();

        examine.setGroupId(homeId);
        ExamineVo exam = examineService.selectByID(examine);
        if (null == exam) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("Examine.record.data.not.exist", null), null);
        }

        // 删除体检表
        int isDele = examineService.delete(examine);
        if (isDele <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("Examine.del.fail", null), 0);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("Examine.del.success", null), isDele);
    }

    /**
     * 修改体检记录接口
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "/updateExamineRecord", method = RequestMethod.POST)
    public Wrapper updateExamineRecord(@Validated(value = {ExamineValid.class}) ExamineDto examine, BindingResult br, HttpServletRequest request) {
        /* 封装需要进行数据校验的字段 */
        List<String> params = new ArrayList<String>();
        params.add("height");
        params.add("weight");
        params.add("SBP");
        params.add("DBP");
        params.add("examinetime");
        params.add("id");
        params.add("examineresult");
        params.add("examinephoto");
        String paramValid[] = new String[params.size()];
        for (int i = 0, j = params.size(); i < j; i++) {
            paramValid[i] = params.get(i);
        }
        /* 存放校验错误结果集 */
        Map<String, String> errorMap = ValidateUtil.getErrorMap(br, paramValid);
        if (errorMap != null && errorMap.size() != 0) {
            /* 返回参数校验错误详情 */
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, MapUtils.getFirstOrNull(errorMap), null);
        }

        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();

        examine.setGroupId(homeId);
        examine.setUserId(loginAuthDto.getUserId());
        examine.setUserName(loginAuthDto.getUserName());

        // 修改体检表
        int n = examineService.update(examine);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("Examine.alert.fail", null), 0);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("Examine.alert.success", null), n);
    }

    /**
     * 查询体检记录接口
     */
    @Authorization
    @ResponseBody
    @RequestMapping(value = "/selExamineRecord", method = RequestMethod.POST)
    public Wrapper selExamineRecord(@RequestBody Map<String, Object> queryParam, HttpServletRequest request) {
        //* 分页数据 *//*
        String pageNo = String.valueOf(queryParam.get("pageNo"));
        String pageSize = String.valueOf(queryParam.get("pageSize"));
        String name = String.valueOf(queryParam.get("name"));
        String examinetime = String.valueOf(queryParam.get("examinetime"));
        if (StringUtils.isNotBlank(name) && (name.length() > 20)) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Examine.query.medical.record.name.len", null), null);
        }
        if (StringUtils.isNotBlank(examinetime) && (examinetime.length() > 20)) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Examine.query.medical.record.date.len", null), null);
        }
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();

        if (StringUtils.isBlank(pageNo)) {
            pageNo = "1";
        }
        if (StringUtils.isBlank(pageSize)) {
            pageSize = "10";
        }
        queryParam.put("homeId", homeId);
        //武昌版平移过来用PageHepler分页插件来分页查询
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        List<ExamineVo> mapList = examineService.examineShowAll(queryParam);
        PageInfo<ExamineVo> info = new PageInfo<>(mapList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("Examine.query.medical.record.success", null), info);
    }

    //根据id查询该老人体检记录
    @Authorization
    @ResponseBody
    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    public Wrapper selectById(ExamineDto examine, HttpServletRequest request) {

        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        examine.setGroupId(homeId);

        ExamineVo exam = examineService.selectByID(examine);
        if (null == exam ) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("Examine.database.not.match.record", null), new ArrayList<>());
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("Examine.query.medical.record.success", null), exam);
    }

    // 查询未体检老人
    @Authorization
    @ResponseBody
    @RequestMapping(value = "/selectOldmans", method = RequestMethod.POST)
    public Wrapper selectOldmans(HttpServletRequest request) {

        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        List<ExamineManVo> mapList = examineService.selectOldmans(homeId);
        if (mapList.size() == 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("comment.query.fail", null), new ArrayList<>());
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("Examine.query.older.success", null), mapList);
    }
}
